﻿package com.firewolf.mybatis.dynamicsql.drivers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;

import com.firewolf.mybatis.dynamicsql.beans.DUser;
import com.firewolf.mybatis.dynamicsql.mapper.DUserMapper;
import com.firewolf.mybatis.dynamicsql.utils.MyBatisUtil;

/**
 * 
 * @Description:
 * @Author: 刘兴
 * @Date : 2016年8月19日
 * @package : com.firewolf.mybatis.dynamicsql.drivers
 * @Java_Version : 1.7
 * @Version : 2016年8月19日下午2:40:17
 */
public class MainDriver {
	
	private SqlSession session = null;
	
	@Before
	public void init() throws IOException{
		session = MyBatisUtil.getSessionFactory("conf.xml").openSession();
	}
	
	@Test
	public void testIf(){
		List<DUser> u = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.if",null);
		System.out.println(u);
	}
	
	@Test
	public void testSwith(){
		DUser us = new DUser();
		us.setAge(12);
		us.setName("zhangsan");
		List<DUser> u = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.switch",us);
		System.out.println(u);
	}
	
	@Test
	public void testWhere(){
		DUser us = new DUser();
//		us.setId(1);
		us.setAge(12);
		us.setName("Tom");
//		List<DUser> u = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.whereError",us);
//		System.out.println(u);
		List<DUser> u2 = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.whereRight",us);

		System.out.println(u2);
	}
	@Test
	public void testTrimWhere(){
		DUser us = new DUser();
//		us.setId(1);
		us.setAge(12);
		us.setName("Tom");
//		List<DUser> u = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.whereError",us);
//		System.out.println(u);
		List<DUser> u2 = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.trimWhere",us);
		
		System.out.println(u2);
	}
	
	@Test
	public void testSet(){
		DUser us = new DUser();
		us.setId(1);
		us.setAge(12);
//		us.setName("Tom");
		session.update("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.setError",us);
	}
	@Test
	public void testTrimSet(){
		DUser us = new DUser();
		us.setId(1);
		us.setAge(12);
//		us.setName("Tom");
		session.update("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.trimSet",us);
	}
	
	@Test
	public void testForEeach(){
		
		List<Integer> ids = new ArrayList<>();
		ids.add(1);
		ids.add(2);
		
		List<DUser> users2 = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.findByIn", ids);
		System.out.println(users2);
		
//		
//		DUserMapper mapper = session.getMapper(DUserMapper.class);
//		List<DUser> users = mapper.findByIn(ids);
//		System.out.println(users);
	}
	
	@Test
	public void testBind(){
		DUser u = new DUser();
		u.setName("om");
		//注意下面传递的参数不能是普通java类型，得是对象，能够提供name的get和set方法
		List<DUser> users2 = session.selectList("com.firewolf.mybatis.dynamicsql.mapper.DUserMapper.bind", u);
		System.out.println(users2);
	}
}
